package cn.itcast.travel.service.impl;

import cn.itcast.travel.dao.FavoriteDao;
import cn.itcast.travel.dao.impl.FavoriteDaoImp;
import cn.itcast.travel.domain.Favorite;
import cn.itcast.travel.domain.PageBean;
import cn.itcast.travel.domain.Route;
import cn.itcast.travel.service.FavoriteService;

import java.util.List;

public class FavoriteServiceImp implements FavoriteService {
private FavoriteDao favoriteDao=new FavoriteDaoImp();
    @Override
    public boolean isFavorite(String rid, int uid) {
        int i = Integer.parseInt(rid);
        Favorite byRidAndUid = favoriteDao.findByRidAndUid(i, uid);
        return byRidAndUid!=null;
    }

    @Override
    public void addFavorite(String rid, int uid) {
        favoriteDao.add(Integer.parseInt(rid),uid);
    }



    @Override
    public PageBean<Route> findMyFavorite(int currentPage, int pageSize, int uid) {
       int  totalCount=favoriteDao.findCountByUid(uid);
        PageBean<Route> routePageBean=new PageBean<>();
        routePageBean.setCurrentPage(currentPage);
       routePageBean.setTotalCount(totalCount);
       routePageBean.setPageSize(pageSize);
        int start=(currentPage-1)*pageSize;
        List<Route> routes = favoriteDao.myFavorite(start, pageSize, uid);
        routePageBean.setList(routes);
        int totalPage=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
        return routePageBean;
    }

    @Override
    public PageBean<Route> findFavoriteQuery(int currentPage, String rname, int sprice, int eprice) {
        PageBean<Route> routePageBean=new PageBean<>();
        int pageSize=8;
        routePageBean.setPageSize(pageSize);
        routePageBean.setCurrentPage(currentPage);
       int  totalCount=favoriteDao.findCountByRse(rname,sprice,eprice);
       routePageBean.setTotalCount(totalCount);
        int start=(currentPage-1)*pageSize;
        int totalPage=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
        routePageBean.setTotalPage(totalPage);
        List<Route>   query=favoriteDao.findFavoriteQuery(start,rname,sprice,eprice,pageSize);
        routePageBean.setList(query);
        return routePageBean;
    }


}
